<!--
 * @Author: delin66
 * @Date: 2025-02-28 20:38:56
 * @LastEditors: delin66
 * @LastEditTime: 2025-05-26 09:42:10
 * @Descripttion: 
-->
<template>
  <div class="container">
    <div class="header">
      <div class="title">园区动态</div>
      <div class="more" @click="more">更多>></div>
    </div>
    <div class="content">
      <div class="box">
        <div class="notice-list">
          <ul>
            <li v-for="item in noticeList" :key="item.id" @click="toDynamicDetail(item.id)">
              <span class="title ellipsis">
                {{ item.title }}
              </span>
              <span class="create-time">{{ item.createTime?.split(' ')[0] ?? '-' }}</span>
            </li>
          </ul>
          <el-empty v-if="total === 0" description="暂无园区动态" />
          <el-pagination
            background
            class="pagination"
            layout="prev, pager, next"
            :total="total"
            :page-size="10"
            v-model:current-page="current"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
      <div class="divide"></div>
      <div class="box">
        <div class="w-full h-full">
          <el-carousel trigger="click" height="400px">
            <el-carousel-item v-for="item in images" :key="item.id">
              <el-tooltip
                class="box-item"
                effect="dark"
                :content="item.title"
                placement="bottom"
              >
                <el-image class="img-dynamic" :src="item.url" :alt="item.title" fit="contain" @click="toDynamicDetail(item.id)" />
              </el-tooltip>
              
            </el-carousel-item>
          </el-carousel>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getAnnouncementList } from '@/api/park';
import { achievementList } from '@/api/technology-transform';
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();

const total = ref(0);
const current = ref<number>(1);
// 获取公告列表
const noticeList = ref();
const images = ref([
  {
    id: 0,
    title: '',
    url: '',
  },
]);

function more () {
  router.push({
    name: 'parkDynamics',
  });
}
// 跳转详情
function toDynamicDetail(id: string | number) {
  const { href } = router.resolve({
    name: 'noticeDetail',
    query: {
      id,
    },
  });
  window.open(href, '_blank');
}

function getNoticeList() {
  const query = {
    type: 5,
    current: current.value,
    status: 1,
  };
  getAnnouncementList(query).then((res: any) => {
    if (res.code === 200) {
      noticeList.value = res.data?.records;
      total.value = res.data?.total;
      
      // 从content中提取第一张图片，最多8张
      images.value = res.data.records
        .filter((record: any) => {
          if (!record.content) return false;
          const imgMatch = record.content.match(/<img[^>]+src="([^">]+)"/);
          return imgMatch !== null;
        })
        .slice(0, 8) // 限制最多8张图片
        .map((record: any) => {
          const imgMatch = record.content.match(/<img[^>]+src="([^">]+)"/);
          return {
            id: record.id,
            title: record.title,
            url: imgMatch ? imgMatch[1] : '',
          };
        });
    }
  });
}

// 分页
function handleCurrentChange(val: number) {
  current.value = val;
  getNoticeList();
}
onMounted(() => {
  getNoticeList();
});
</script>

<style scoped lang="scss">
.container {
  margin-top: 30px;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 500px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  .header {
    display: flex;
    height: 50px;
    line-height: 50px;
    text-align: center;
    font-size: 25px;
    font-weight: bold;
    color: white;
    background-color: #0370c2;
    clip-path: polygon(0 0, 100% 0, 100% 85%, 96% 85%, 96% 100%, 4% 100%, 4% 85%, 0 85%);
    .title {
      margin: 0 auto;
      font-weight: bolder;
    }
    .more {
      margin-right: 40px;
      font-weight: bold;
      font-size: 16px;
      cursor: pointer;
      z-index: 99;
    }
  }

  .content {
    height: 450px;
    display: flex;
    justify-content: space-between;
    .box {
      width: 50%;
      .sub_title {
        width: 60%;
        margin: 10px auto;
        text-align: center;
        line-height: 40px;
        font-size: 20px;
        font-weight: bold;
        border-bottom: 2px #338ccb solid;
      }
      .img-dynamic {
        height: 100%;
        width: 100%;
        cursor: pointer;
      }
    }
    .divide {
      margin: auto 0;
      height: 80%;
      width: 1px;
      background-color: rgba(0, 0, 0, 0.1);
    }
  }
}
.notice-list {
  padding: 0 30px;
  flex: 1;
  width: 100%;
  padding-top: 30px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  li {
    position: relative;
    padding: 0 5%;
    margin-bottom: 5px;
    display: flex;
    justify-content: space-between;
    &::before {
      content: '';
      position: absolute;
      width: 5px;
      height: 5px;
      background-color: #338ccb;
      top: 50%;
      left: 2.5%;
      transform: translate(-50%, -50%);
    }
    .title {
      margin-left: 10px;
      max-width: 85%;
      font-size: 16px;
      &:hover {
        cursor: pointer;
        color: #338ccb;
      }
    }
    .create-time {
      text-wrap: nowrap;
    }
  }
}
.pagination {
  margin: 10px 0;
  display: flex;
  justify-content: flex-end;
}
</style>
